package cn.itsource.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.itsource.domain.Course;
import cn.itsource.mapper.CourseMapper;
import cn.itsource.query.Query;
import cn.itsource.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;


    @Override
    public List<Course> findAll() {

        return courseMapper.findAll();
    }

    @Override
    public Course findOne(Long id) {
        return courseMapper.findOne(id);
    }

    @Override
    public void add(Course course) {
        courseMapper.add(course);
    }

    @Override
    public void update(Course course) {
        courseMapper.update(course);
    }

    @Override
    public void delete(Long id) {
        courseMapper.delete(id);
    }

    @Override
    public PageInfo<Course> pageQuery(Query query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Course> courseList = courseMapper.pageQuery(query);
        return new PageInfo<>(courseList);
    }

    @Override
    public void patchdel(Long[] ids) {
        courseMapper.patchdel(ids);
    }

    //导出文件
    @Override
    public void exportFile(Long[] ids) { //ids
        ArrayList<Course> arrayList = new ArrayList<Course>();
        for (Long id : ids) {
            Course course = courseMapper.findOne(id);
            arrayList.add(course);
        }
        ArrayList<Course> courses = CollUtil.newArrayList(arrayList);
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("d:/course.xlsx");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(courses, true);
        // 关闭writer，释放内存
        writer.close();
    }
}
